//手写一个死锁代码
//提供：可以通过 synchronized 或者 Lock 来实现一个死锁代码。
//所谓的死锁就是线程一拥有锁1，线程二拥有锁2，双方在拥有自身锁的同时尝试获取对方的锁，最终两个线程就会进入无线等待的状态，这就是死锁。
public class Homework2 {
    public static void main(String[] args) throws InterruptedException {
        //先提供两个锁
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{
           synchronized (locker1){
               try {
                   Thread.sleep(1000);
               }catch (InterruptedException e){
                   e.printStackTrace();
               }
               //开始第二个锁
               synchronized (locker2){
                   System.out.println("t1成功获取两把锁!");
               }
           }
        });

        Thread t2 = new Thread(()->{
           synchronized (locker2){
               try {
                   Thread.sleep(1000);
               }catch (InterruptedException e){
                   e.printStackTrace();
               }
               synchronized (locker1){
                   System.out.println("t2成功获取两把锁!");
               }
           }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("程序运行完毕!");
    }
}
